<?php
class BaseCateController extends ManagerController {
	var $cate_name = '';
	var $cate_mode = null;
	var $list_url = null;
	public function BaseCateController(){
		parent::ManagerController();
		if(empty($this->cate_mode)){
			$this->cate_mode = new MongoModel();
			$this->cate_mode->table = strtolower(APP);
		}
		if(empty($this->list_url)){
			$this->list_url = '/manager.php?app=' . strtolower(APP);
		}
		$this->assign('cate_code', preg_replace('/^(.*)_cate$/i', '$1', strtolower(APP)));
		$this->assign('cate_name', $this->cate_name);
	}
	
	public function index(){
		$this->page_title = $this->cate_name . '分类管理 - '. $this->site_info['name'];
		$cates = $this->get_cates(SITE_ID, 'name,parent,'.LANGUAGE.'.name');
		$tree = new Tree($cates);
		$this->assign('cates', $tree->get_formated_list());
		$this->display();
	}
	
	public function del(){
		if(!isset($this->id)){
			$this->ajax_error('未指定删除记录！');
		}
		
		$cate = $this->get_cate_by_key($this->id, 'parent');
		if(empty($cate)){
			$this->ajax_error('要删除的记录不存在或者已经删除！');
		}
		//将原来下面的分类向上移
		$this->cate_mode->update_by_attrs(array('parent'=>$this->id, 'site_id'=>SITE_ID),array('parent'=>$cate['parent']));
		
		$this->cate_mode->delete_by_key($this->id);
		$this->ajax_info('删除成功！');
	}
	
	public function edit(){
		if(IS_POST){
			//保存
			if($this->save()){
				//如果正常保存，则直接跳转回分类列表页面
				$this->redirect($this->list_url);
			}
		}
		$this->page_title = $this->cate_name . '分类编辑 - '. $this->site_info['name'];
		if(isset($_GET['lang'])){
			$lang = $this->get_val('lang');
		}else{
			$lang = LANGUAGE;
		}
		$art_cates = $this->get_cates(SITE_ID, 'parent,' . LANGUAGE . '.name');
		$tree = new Tree($art_cates);
		//加载所有分类
		$this->assign('cate_tree', $tree->get_options('　'));
		
		//加载会员级别
		$user_level_mod = new User_levelModel();
		$uls = $user_level_mod->get_member_level_options(array('site_id'=>SITE_ID), $lang);
		$this->assign('user_levels', $uls);
		
		if(isset($this->id)){
			//修改，获取当前记录
			$cate = $this->get_cate_by_key($this->id);
			$this->assign('parent', $cate['parent']);
			$this->assign('cate', $cate);
		}
		if(isset($_GET['parent'])){
			//添加分类
			$parent_id = $this->get_val('parent');
			$this->assign('parent', $parent_id);
		}
		$attrs_mod = new AttrsModel();
		$attrs = $attrs_mod->find(array('site_id'=>SITE_ID), 'name');
		$tree = new Tree($attrs);
		$this->assign('attrs', $tree->get_options('　'));
		
		//添加
		$this->display();
	}
	
	private function save(){
		$name = $this->get_post_val('name');
		$name_len = utf8_strlen($name);
		if($name_len<1 || $name_len>50){
			$this->assign('error', '分类名称长度应该在1至50之间！');
			return false;
		}
		
		$parent = $this->get_post_val('parent');
		
		if(empty($this->id)){
			//新建的分类
			$base_path = $this->get_post_val('base_path', null);
			$pinyin = $this->get_post_int_val('pinyin', 0);
			if($pinyin != 0){
				//使用拼音
				$path = Pinyin::cn2pinyin($name);
			}else{
				$path = $this->get_post_val('path');
				if(empty($path)){
					//如果没有设置path，则使用拼音作为它的路径 
					$path = Pinyin::cn2pinyin($name);
				}
			}
			if(empty($path)){
				//如果还是为空，则使用默认的article作为路径 
				$path = 'article';
			}
			if($base_path == 'site_base'){
				//如果是基于网站
				$path = '/' . $path;
			}else{
				//基于上级目录
				if($parent == '0'){
					//顶级目录
					$path = '/' . $path;
				}else{
					//如果不是顶级目录，则获取目录
					$condition = array('site_id'=>SITE_ID, '_id'=>$parent);
					$parent_cate = $this->cate_mode->get_by_attrs($condition, 'path');
					if(empty($parent_cate)){
						//如果不空
						$this->assign('error', '父级分类不存在！');
						return false;
					}
					$path = $parent_cate['path'] . '/' . $path;
				}
			}
		}else{
			//直接取
			$path = $this->get_post_val('path');
		}
		
		$user_level = $_POST['user_level'];
		$seo_title = $this->get_post_val('seo_title');
		$seo_description = $this->get_post_val('seo_description');
		$seo_keywords = $this->get_post_val('seo_keywords');
		$template_index = $this->get_post_val('template_index', 'article.index.htm');
		$template_list = $this->get_post_val('template_list', 'article.list.htm');
		$template_detail = $this->get_post_val('template_detail', 'article.detail.htm');
		$desc = $this->get_post_val('desc');
		
		$base_cate = array(
			LANGUAGE => array(
				'name' => $name,
			),
			'path' => $path,
			'site_id' => SITE_ID,
		);
		if(!empty($parent)){
			$base_cate['parent'] = $parent;
		}
		if(!empty($user_level)){
			$base_cate['user_level'] = $user_level;
		}
		if(!empty($seo_title)){
			$base_cate[LANGUAGE]['seo_title'] = $seo_title;
		}
		if(!empty($seo_description)){
			$base_cate[LANGUAGE]['seo_description'] = $seo_description;
		}
		if(!empty($seo_keywords)){
			$base_cate[LANGUAGE]['seo_keywords'] = $seo_keywords;
		}
	
		if(!empty($template_index)){
			$base_cate['template']['template_index'] = $template_index;
		}
		if(!empty($template_list)){
			$base_cate['template']['template_list'] = $template_list;
		}
		if(!empty($template_detail)){
			$base_cate['template']['template_detail'] = $template_detail;
		}
		if(!empty($desc)){
			$base_cate['desc'] = $desc;
		}
	
		//属性组
		$attrs = $_POST['attrs'];
		if(!empty($attrs)){
			$attr_list = array();
			foreach($attrs as $c){
				$attr_list[$c] = 10000;
			}
			$base_cate['attr_groups'] = $attr_list;
		}
		
		if(empty($this->id) || $this->id == '0'){
			//添加
			$this->cate_mode->insert($base_cate);
		}else{
			$this->cate_mode->update_by_key($this->id, $base_cate);
			$base_cate['_id'] = $this->id;
		}
		return $base_cate;
	}
	
    protected function get_cate_by_key($key, $lang=null, $columns=null){
    	if($lang==null)$lang = LANGUAGE;
    	$cate = $this->cate_mode->get_by_key($key, $columns);
    	if(empty($cate))
    		return null;
    	if(isset($cate[$lang])){
    		foreach($cate[$lang] as $k=>$v){
    			$cate[$k] = $v;
    		}
    		unset($cate[$lang]);
    	}
    	return $cate;
    }
    
    protected function get_cates($site_id, $columns=null, $lang=null){
    	if($lang==null) $lang = LANGUAGE;
    	$cates = $this->cate_mode->find(array('site_id'=>$site_id), $columns);
    	foreach($cates as $cate_id=>&$cate){
	    	if(isset($cate[$lang])){
	    		foreach($cate[$lang] as $k=>$v){
	    			$cate[$k] = $v;
	    		}
	    		unset($cate[$lang]);
	    	}
    	}
    	return $cates;
    }
}
?>